package com.hndct.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static Date getDateTimeFromStr(String endTime) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.parse(endTime);
	}
	
	/*public static int getHourByDate(Date date){
		return date.getHours();
	}*/
	
	public static Date getDateBeforeHour(Date date, int hour) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.HOUR_OF_DAY, -hour);
		return c.getTime();
	}
	
	public static int getHourNow(){
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.HOUR_OF_DAY);
	}
	public static int getHour(Date time){
		Calendar c = Calendar.getInstance();
    	c.setTime(time);
		return c.get(Calendar.HOUR_OF_DAY);
	}
	
	public static int getMinuteNow(){
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.MINUTE);
	}
	
	public static int getMinute(Date time){
		Calendar c = Calendar.getInstance();
    	c.setTime(time);
		return c.get(Calendar.MINUTE);
	}
	
	public static Date getDateFromStr(String endTime) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.parse(endTime);
	}
	
	public static String getDaysBefore(int n){
    	Date today = new Date();
    	Calendar c = Calendar.getInstance();
    	c.setTime(today);
    	c.add(Calendar.DATE, -n);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
    	return dft.format(c.getTime());
    }
	public static String getDaysBefore(Date date,int n){
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.add(Calendar.DATE, -n);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
    	return dft.format(c.getTime());
    }
	
	public static String getDaysBefore(int n,String dfts){
    	Date today = new Date();
    	Calendar c = Calendar.getInstance();
    	c.setTime(today);
    	c.add(Calendar.DATE, -n);
    	SimpleDateFormat dft = new SimpleDateFormat(dfts);
    	return dft.format(c.getTime());
    }
	public static String getDaysBefore(Date date,int n,String dfts){
    	Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	c.add(Calendar.DATE, -n);
    	SimpleDateFormat dft = new SimpleDateFormat(dfts);
    	return dft.format(c.getTime());
    }
	
	public static String getMonthsWithYearBefore(int n) {
		Calendar c = Calendar.getInstance();
    	c.add(Calendar.MONTH, -n);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
		return dft.format(c.getTime());
	}
	
	/**
	 * 获取指定分钟前date对象
	 */
	public static Date getDateBeforeMin(int min){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MINUTE, -min);
		return c.getTime();
	}
	
	/**
	 * 获取指定小时前date对象
	 */
	public static Date getDateBeforeHour(int hour) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, -hour);
		return c.getTime();
	}
	
	public static String getStrFromDate(Date date){
		Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
    	return dft.format(c.getTime());
	}
	
	public static String getStrFromDateWithSecond(Date date){
		Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return dft.format(c.getTime());
	}

	public static String getStrFromDate(Date date, String splitBy) {
		Calendar c = Calendar.getInstance();
    	c.setTime(date);
    	SimpleDateFormat dft = new SimpleDateFormat("yyyy"+splitBy+"MM"+splitBy+"dd");
    	return dft.format(c.getTime());
	}
	
	/**
	 * 获取当前时间是今年的第几周
	 */
	public static int getWeekOfYear(){
		Calendar c = Calendar.getInstance();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * 获取现在是哪一年
	 */
	public static int getYear() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR);
	}
	
	public static int getYear(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		return c.get(Calendar.YEAR);
	}
	
	/**
	 * 获取现在是哪个月
	 */
	public static int getMonth(){
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.MONTH)+1;
	}
	
	public static int getMonth(Date time){
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		return c.get(Calendar.MONTH)+1;
	}

//	public static void main(String[] args) {
//		System.out.println(getMonthsWithYearBefore(0));
//		System.out.println(getMonthsWithYearBefore(1));
//		System.out.println(getMonthsWithYearBefore(2));
//		System.out.println(getMonthsWithYearBefore(3));
//		System.out.println(getMonthsWithYearBefore(4));
//		System.out.println(getMonthsWithYearBefore(5));
//	}
	
}
